package cn.sccl.dirms.dev.controller;

import cn.sccl.common.model.PageResult;
import cn.sccl.common.util.DevLog;
import cn.sccl.common.web.BaseController;
import cn.sccl.dirms.dev.model.ChartData;
import cn.sccl.dirms.dev.model.ChartQuery;
import org.springframework.beans.factory.annotation.Autowired;
import com.fasterxml.jackson.annotation.JsonView;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import cn.sccl.dirms.dev.model.Murri;
import cn.sccl.dirms.dev.service.MurriManager;

/**
 * 的Controller
 * Created by liupan on 2017-06-25.
 */
@RestController
@RequestMapping(value = "/dirms/dev/murri")
public class MurriController extends BaseController {

    @Autowired
    private MurriManager murriManager;

    /**
     * 显示所有数据
     */
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<Murri> listAllMurri() {
        return murriManager.listAll();
    }

    /**
     * 分页查询，分页参数放置在url路径中
     *
     * @param pageIndex 页码
     * @param pageSize  分页大小
     * @param request
     * @return
     */
    @RequestMapping(value = "/page/{pageIndex}/{pageSize}", method = RequestMethod.GET)
    public PageResult<Murri> pageQueryMurri(@PathVariable("pageIndex") long pageIndex, @PathVariable("pageSize") int pageSize, HttpServletRequest request) {
        return murriManager.pageQueryByLike(pageIndex, pageSize, getQueryData(request));
    }

    /**
     * 分页查询，分页参数放置在request中
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public PageResult<Murri> pageQueryMurri(HttpServletRequest request) {
        return murriManager.pageQueryByLike(getPageIndex(request), getPageSize(request), getQueryData(request));
    }

    /**
     * 保存数据
     *
     * @param murri
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Murri saveMurri(@RequestBody Murri murri) {
        murriManager.save(murri);
        return murri;
    }

    /**
     * 根据ID删除数据
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @JsonView(Object.class)
    public void deleteMurri(@PathVariable("id") String id) {
        murriManager.delete(id);
    }


    /**
     * 根据配置编号获取图标信息
     *
     * @param graphNo
     */
    @RequestMapping(value = "/{graphNo}", method = RequestMethod.GET)
    public List<Murri> getGraph(@PathVariable("graphNo") String graphNo) {
        return murriManager.getGraph(graphNo);
    }

    /**
     * 根据图标ID获取数据
     *
     * @param id
     */
    @RequestMapping(value = "/viewData/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ChartData viewData(@PathVariable("id") String id, @RequestBody ChartQuery query) {
        try {
            return murriManager.viewData(id, query);
        } catch (Exception e) {
            DevLog.error(e);
            return null;
        }
    }
}